/*
 * Copyright (c) 2011-2014, fortiss GmbH.
 * Licensed under the Apache License, Version 2.0.
 *
 * Use, modification and distribution are subject to the terms specified
 * in the accompanying license file LICENSE.txt located at the root directory
 * of this software distribution. A copy is available at
 * http://chromosome.fortiss.org/.
 *
 * This file is part of CHROMOSOME.
 *
 * $Id: plugAndPlayClientProcessGraphFunction.c 7837 2014-03-14 12:33:13Z wiesmueller $
 */

/**
 * \file
 *         Source file for function pnpClientProcessGraph in component pnpClient.
 *
 * \author
 *         This file has been generated by the CHROMOSOME Modeling Tool (XMT)
 *         (fortiss GmbH).
 */

/******************************************************************************/
/***   Includes                                                             ***/
/******************************************************************************/
#include "xme/core/plugAndPlay/include-gen/plugAndPlayClientProcessGraphFunction.h"

#include "xme/core/plugAndPlay/include-gen/plugAndPlayClientProcessGraphFunctionWrapper.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayClientComponent.h"
#include "xme/core/plugAndPlay/include-gen/plugAndPlayClientComponentWrapper.h"
#include "xme/core/plugAndPlay/include-gen/pnpClientManifest.h"

#include "xme/core/logUtils.h"

#include "xme/hal/include/mem.h"

// PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_C_INCLUDES) ENABLED START

#include "xme/core/plugAndPlay/include/plugAndPlayClient.h"
#include "xme/core/executionManager/include/executionManager.h"

// PROTECTED REGION END

/******************************************************************************/
/***   Definitions                                                          ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_C_DEFINITIONS) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Variables                                                            ***/
/******************************************************************************/
/**
 * \brief  Variable holding the value of the optional output port 'outLogoutAcknowledgment'.
 *
 * \details If necessary initialize this in the init function.
 *          The value of this variable will be written to the port at the end of
 *          the step function.
 */
static xme_core_topic_pnp_logoutAcknowledgment_t
portOutLogoutAcknowledgmentData;

// PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_C_VARIABLES) ENABLED START

/**
 * \brief Flag indicating whether a shutdown of this node should occur.
 */
static char isShutdownDesired = false;

// PROTECTED REGION END

/******************************************************************************/
/***   Prototypes                                                           ***/
/******************************************************************************/

// PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_C_PROTOTYPES) ENABLED START

// PROTECTED REGION END

/******************************************************************************/
/***   Implementation                                                       ***/
/******************************************************************************/
xme_status_t
xme_core_pnp_pnpClientProcessGraphFunction_init
(
    xme_core_pnp_pnpClientComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_INITIALIZE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    return XME_STATUS_SUCCESS;

    // PROTECTED REGION END
}

void
xme_core_pnp_pnpClientProcessGraphFunction_step
(
    xme_core_pnp_pnpClientComponent_config_t* const componentConfig
)
{
    xme_status_t status[1];
    
    xme_core_topic_pnpManager_runtime_graph_model_t portInGraphData; // Required port.
    xme_core_topic_pnp_logoutAcknowledgment_t* portOutLogoutAcknowledgmentDataPtr = &portOutLogoutAcknowledgmentData;
    
    (void)xme_hal_mem_set(&portInGraphData, 0u, sizeof(xme_core_topic_pnpManager_runtime_graph_model_t));
    
    status[0] = xme_core_pnp_pnpClientComponentWrapper_readPortInGraph(&portInGraphData);
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_STEP_C) ENABLED START

        // FIXME: Change this function behaviour.

        XME_UNUSED_PARAMETER(componentConfig);

        portOutLogoutAcknowledgmentDataPtr = NULL;

        XME_CHECK_MSG
        (
            XME_STATUS_SUCCESS == status[0] ||
            XME_STATUS_NO_SUCH_VALUE == status[0], // This parameter is when we are not producing data. 
            XME_CHECK_RVAL_VOID,
            XME_LOG_WARNING,
            "[PnPClient] The read input runtime graph failed.\n"
        );

        if(XME_STATUS_SUCCESS == status[0])
        {
            XME_CHECK_MSG
            (
                XME_STATUS_SUCCESS == xme_core_pnp_pnpClient_processGraph((xme_core_topic_pnpManager_runtime_graph_model_t *)&portInGraphData, &isShutdownDesired),
                XME_CHECK_RVAL_VOID,
                XME_LOG_ERROR,
                "[PnPClient] Unable to process the incoming graph.\n"
            );
        }
        if (isShutdownDesired)
        {
            portOutLogoutAcknowledgmentDataPtr = &portOutLogoutAcknowledgmentData;
            portOutLogoutAcknowledgmentDataPtr->nodeId = xme_core_node_getCurrentNodeId();
        }

        // PROTECTED REGION END
    }
    
    status[0] = xme_core_pnp_pnpClientComponentWrapper_writePortOutLogoutAcknowledgment(portOutLogoutAcknowledgmentDataPtr);
    
    {
        // PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_STEP_2_C) ENABLED START
    
        if (isShutdownDesired)
        {
            if (XME_STATUS_SUCCESS != status[0])
               XME_LOG(XME_LOG_ERROR, "[PnPClient] LogOut Acknowldgement was not sent\n");
            //TODO there is a bit of trouble here.
            //we will stop at the end of this cycle but what about if the current request is processed at the beginning of next cycle?
            //that is the ack is sent in next cycle, then it will never hit the wire
            xme_core_exec_stop(true);
        }
    
        // PROTECTED REGION END
    }
}

void
xme_core_pnp_pnpClientProcessGraphFunction_fini
(
    xme_core_pnp_pnpClientComponent_config_t* const componentConfig
)
{
    // PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_TERMINATE_C) ENABLED START

    // Nothing to do

    XME_UNUSED_PARAMETER(componentConfig);

    // PROTECTED REGION END
}

// PROTECTED REGION ID(XME_CORE_PNP_PNPCLIENTPROCESSGRAPHFUNCTION_IMPLEMENTATION_C) ENABLED START

// PROTECTED REGION END
